package com.prabodha.fruitmap;


import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;

import com.prabodha.fruitmap.R.id;
import com.webService.Webservice;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
static String userid;
String userRole;
String username;
String password;
SoapObject request;
SoapSerializationEnvelope envelope;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button newuser=(Button)findViewById(R.id.button1);
        
        Button login=(Button)findViewById(R.id.button2);
        final EditText user=(EditText) findViewById(R.id.editText1);
        final EditText pass=(EditText) findViewById(R.id.editText2);
        
        
        
        login.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				username= user.getText().toString();
        		password=pass.getText().toString();
                                
                if((!(username==null))&&(!(password==null))){
              	  
              	  try{
                    	Webservice wb = new Webservice();
                    	wb.METHOD_NAME = "isValiedUser";
                    	wb.SOAP_ACTION = "http://fruitMap.com/"+wb.METHOD_NAME;
                    	 
                    	 request =wb.getRequest();
                    	 request.addProperty("userName",username);
                    	 request.addProperty("passwd",password);
                    	 
                    	 envelope = wb.setRequestAndGetEnvilop(request); 
                    	 SoapPrimitive soap = wb.getPrimitive(envelope);
                    	 String idRole=soap.toString();
                    	 userRole=idRole.charAt(idRole.length()-1)+"";  // Getting User Role
                    	 int length=soap.toString().length();
                    	 userid=soap.toString().substring(0, length-2);
                    	 
                    	 
                    	 
                    	 Log.d("userID and role", userid);
                    	 
                    	 if(Integer.parseInt(soap.toString().trim())>0){
                    		//showDialog("Successfully login !");
                    		//userid=Integer.parseInt(soap.toString().trim());
                    		Log.d("login", "Logedd in yapee");
                    		
                    		Intent intent = new Intent(MainActivity.this, TabsActivity.class);
                    		startActivity(intent);
                    		
                    		
                    		 //Toast.makeText(this.MainActivity, "Loged in",Toast.LENGTH_SHORT ).show();
                    		Intent in = new Intent(MainActivity.this, TabsActivity.class);
                    		//in.setClass(this,TabsActivity.class);
                    		startActivity(in);
                    		// new StoreMapVariable().setVaraible();
                    		//Toast.makeText(this,"true",Toast.LENGTH_SHORT).show(); 
                    	 }
                    	 else{
                    		//showDialog("Enter valied User name and password  !");
                    		//Toast.makeText(this,"false",Toast.LENGTH_SHORT).show();
                    		 Log.d("login", "NOT loged in.... FUCK....");
                    	 }
                    
                    }catch(Exception ex){}
             
              	  
                }else{
              	// showDialog("Fill the user name and password");
              	//new Decarate().showDialog("Fill the user name and password");
              	// Toast.makeText(this,"Fill the user name and password",Toast.LENGTH_SHORT).show();  
              	  
                }
				
				
			}
			});
        
        
        newuser.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				
				Intent intent = new Intent(MainActivity.this, NewUser.class);
        		startActivity(intent);
              
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
